package com.ks.core.framework.cpsupload.demos.orderpayinvoice.paytemplate;

import com.ks.core.framework.cpsupload.demos.orderpayinvoice.constant.PaymentType;
import com.ks.core.framework.cpsupload.demos.orderpayinvoice.orderhandler.Order;
import com.ks.core.framework.cpsupload.demos.orderpayinvoice.paystrategy.PaymentStrategy;
import com.ks.core.framework.cpsupload.demos.orderpayinvoice.paystrategy.PaymentStrategyFactory;

import java.math.BigDecimal;

// 模板方法：支付流程模板
public abstract class PaymentTemplate {
    public final boolean processPayment(Order order, PaymentType type, BigDecimal amount) {
        // 1. 验证
        if (!validate(order, type, amount)) {
            return false;
        }
        
        // 2. 执行支付
        PaymentStrategy strategy = PaymentStrategyFactory.getStrategy(type);
        boolean success = strategy.pay(order.getOrderId(), amount);
        
        // 3. 处理后处理
        postProcess(order, type, amount, success);
        
        return success;
    }
    
    protected abstract boolean validate(Order order, PaymentType type, BigDecimal amount);
    
    protected abstract void postProcess(Order order, PaymentType type, BigDecimal amount, boolean success);
}